﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.admin.shop
{
    public partial class topics_add : System.Web.UI.Page
    {

        // Methods
        protected void Button1_Click(object sender, EventArgs e)
        {
            upload upload = new upload();
            string str = upload.Up(this.file1, "../../uploadfile/flash/");
            base.Response.Write(upload.Resup[Convert.ToInt32(str)]);
            this.pic.Text = "/uploadfile/flash/" + upload.s;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (base.Request.QueryString["tid"] != null)
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_Topic set topic='", re.replace(this.topic.Value), "',topicopen=", re.replace(this.topicopen.SelectedValue), ",topictitle='", re.replace(this.topictitle.Value), "',topicpage=", re.replace(this.topicpage.Value), ",topicorder=", re.replace(this.topicorder.Value), ",isnew=", this.isnew.Checked  ? "1" : "0", ",visible=", this.visible.Checked  ? "1" : "0", ",pic='", re.replace(this.pic.Text), 
                "',picurl='", re.replace(this.picurl.Value), "',isflash=",  this.isflash.Checked  ? "1" : "0", ",languages=", info.languages(""), " where topicid=", re.replace(base.Request.QueryString["tid"]), ""
             }));
                base.Response.Write("<script language='javascript'>alert('专题信息修改成功');window.location.href='topics.aspx';</script>");
                base.Response.End();
            }
            else
            {
                long num;
                string sql = "select * from DB_Topic order by topicid desc";
                DataTable table = new DataTable();
                table = SQLAccess.GreatDs(sql).Tables[0];
                if (table.Rows.Count > 0)
                {
                    num = Convert.ToInt32(table.Rows[0]["topicid"]) + 1;
                }
                else
                {
                    num = 1L;
                }
                SQLAccess.DoSql(string.Concat(new object[] { 
                "insert into DB_Topic (topicid,topic,topicopen,topictitle,topicpage,topicorder,isnew,visible,pic,picurl,isflash,location,languages) values (", num.ToString(), ",'", re.replace(this.topic.Value), "',", re.replace(this.topicopen.SelectedValue), ",'", re.replace(this.topictitle.Value), "',", re.replace(this.topicpage.Value), ",", re.replace(this.topicorder.Value), ",", this.isnew.Checked ? "1":"0", ",", this.visible.Checked? "1":"0", 
                ",'", re.replace(this.pic.Text), "','", re.replace(this.picurl.Value), "',", this.isflash.Checked? "1":"0", ",1,", info.languages(""), ")"
             }));
                if (this.isflash.Checked)
                {
                    SQLAccess.DoSql("insert into DB_Flash (flashname,flashpic,flashurl,visible,topicid,modelid) values ('" + re.replace(this.topic.Value) + "','" + re.replace(this.pic.Text) + "','" + re.replace(this.picurl.Value) + "?topic_id=" + num.ToString() + "',1," + num.ToString() + "," + info.model("") + ")");
                }
                base.Response.Write("<script language='javascript'>alert('专题信息添加成功');window.location.href='topic_add.aspx';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton1.Attributes["onclick"] = "return CheckFrm();";
            if (!base.IsPostBack && (base.Request.QueryString["tid"] != null))
            {
                this.upbind();
            }
        }

        private void upbind()
        {
            DataTable table = SQLAccess.GreatDs("select * from DB_Topic where topicid=" + re.replace(base.Request.QueryString["tid"]) + "").Tables[0];
            if (table.Rows.Count > 0)
            {
                this.topic.Value = table.Rows[0]["topic"].ToString();
                this.time1.Value = table.Rows[0]["time1"].ToString();
                this.time2.Value = table.Rows[0]["time2"].ToString();
                this.pic.Text = table.Rows[0]["pic"].ToString();
                this.picurl.Value = table.Rows[0]["picurl"].ToString();
                this.topicorder.Value = table.Rows[0]["topicorder"].ToString();
                this.topicpage.Value = table.Rows[0]["topicpage"].ToString();
                this.topictitle.Value = table.Rows[0]["topictitle"].ToString();
                this.topicopen.SelectedValue = table.Rows[0]["topicopen"].ToString();
                this.location.SelectedValue = table.Rows[0]["location"].ToString();
                this.isnew.Checked = (bool)table.Rows[0]["isnew"];
                this.visible.Checked = (bool)table.Rows[0]["visible"];
                this.isflash.Checked = (bool)table.Rows[0]["visible"];
            }
        }



    }
}